<?php

function exception_error_handler($errno, $errstr, $errfile, $errline )
{
	try {
		throw new CS_Error_Exception($errstr, 0, $errno, $errfile, $errline);
	} catch(CS_Error_Exception $e) {
		$e->printError();
	}
}

function __autoLoad($class_name)
{
	$class = str_replace('_', DS, $class_name).EXT;

	if(class_exists($class_name))
	{
		return true;
	} else {
		try {				
			if(file_exists(PATH.LIBS.DS.$class))
			{
				require_once(PATH.LIBS.DS.$class);
			} else {
				throw new CS_Error_Exception('The specified class does not exist : '.$class_name);
			}
		} catch(CS_Error_Exception $e)
		{
			$e->printError();
		} catch(Exception $e)
		{
			echo $e->getMessage();
		}
	}
}

function shutdown()
{
	// TODO : display benchmarking, sql dump, debug info etc.
	// TODO : close database connection
}


function debug($var, $exit = false)
{

	$debug = debug_backtrace();
	echo '<pre>';

	echo '<b>'.str_replace(PATH, '', $debug[0]['file']).' : '.$debug[0]['line'].'</b><br /><br />';	

	if(is_object($var) || is_array($var) || is_string($var))
	{
		print_r($var);
	} else {
		var_dump($var);
	}

	echo '</pre><br /><hr /><br />';

	if($exit)
	{
		exit();
	}
}
